<template>
  <!-- 地图管理组件 -->
  <div class="map">
    <!-- 面包屑路径导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>世界管理</el-breadcrumb-item>
      <el-breadcrumb-item>地图列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容区 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <div class="grid-content search-container">
            <el-input
              placeholder="请输入内容"
              v-model.trim="searchCont"
              clearable
              class="search-input"
              @clear="searchClear"
            >
            </el-input>
            <el-button
              type="info"
              plain
              icon="el-icon-search"
              @click="getMapList"
            ></el-button>
          </div>
        </el-col>
      </el-row>
      <!-- 地图列表内容 -->
      <div class="map-table">
        <el-table :data="mapList" border style="width: 100%">
          <el-table-column type="index" label="#" width="48"></el-table-column>
          <el-table-column prop="map_id" label="地图ID"></el-table-column>
          <el-table-column label="地图预览">
            <template slot-scope="props">
              <div v-if="props.row.map_img">
                <el-image :src="path + props.row.map_img">
                  <span slot="error">无地图预览</span>
                </el-image>
              </div>
              <div v-else>
                无地图预览
              </div>
            </template>
          </el-table-column>
          <el-table-column label="所属区域">
            <template slot-scope="props">
              <div>
                <img
                  :src="path + props.row.map_mark"
                  @error="errorImg($event)"
                  v-if="props.row.map_mark"
                  style="vertical-align:middle;margin-right:5px;"
                />
                <span>{{ props.row.map_street }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="map_name" label="地图名称"></el-table-column>
          <el-table-column label="地图描述">
            <template slot-scope="props">
              <div v-if="props.row.map_desc">
                {{ props.row.map_desc }}
              </div>
              <div v-else>
                无
              </div>
            </template>
          </el-table-column>
          <el-table-column label="背景音乐">
            <template slot-scope="props">
              <div v-if="props.row.map_bgm">
                <span>{{
                  props.row.map_bgm.substr(props.row.map_bgm.indexOf('/') + 1)
                }}</span>
                <a
                  href="javascript:;"
                  class="iconfont icon-bofang1 play-music"
                  @click="
                    playMusic(
                      props.row.map_bgm,
                      props.row.map_bgm.substr(
                        props.row.map_bgm.indexOf('/') + 1
                      )
                    )
                  "
                ></a>
              </div>
              <div v-else>
                无
              </div>
            </template>
          </el-table-column>
          <el-table-column label="地图开关">
            <template slot-scope="props">
              <div>
                <el-switch
                  style="display: block"
                  v-model="props.row.map_status"
                  @change="switchMap(props.row)"
                >
                </el-switch>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="props">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="editMap(props.row.map_id)"
              ></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deleteMap(props.row.map_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageConfig.currentPage"
          :page-sizes="pageConfig.pageSizes"
          :page-size="pageConfig.pageSize"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageConfig.pageTotal"
          :hide-on-single-page="false"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 修改地图弹出层 -->
    <el-dialog
      title="编辑地图"
      :visible.sync="dialogConfig.dialogEditMapVisible"
      class="edit-map-dialog"
    >
      <el-form
        :model="editMapForm"
        :rules="editMapFormRules"
        ref="editMapForm"
        class="demo-ruleForm"
        :label-position="'right'"
        label-width="80px"
      >
        <el-form-item label="地图ID">
          <el-input v-model.trim="editMapForm.mapId" disabled></el-input>
        </el-form-item>
        <el-form-item label="所属区域">
          <el-input v-model.trim="editMapForm.mapStreet" disabled></el-input>
        </el-form-item>
        <el-form-item label="地图名称" prop="mapName">
          <el-input v-model.trim="editMapForm.mapName"></el-input>
        </el-form-item>
        <el-form-item label="地图描述" prop="mapDesc">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="editMapForm.mapDesc"
            resize="none"
          >
          </el-input>
        </el-form-item>
        <el-form-item style="text-align: right">
          <el-button @click="dialogConfig.dialogEditMapVisible = false"
            >取消</el-button
          >
          <el-button type="primary" @click="editMapSubmit('editMapForm')"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索内容
      searchCont: '',
      // 地图列表数据
      mapList: [],
      // 资源路径
      path: '',
      // 分页器参数配置
      pageConfig: {
        pageTotal: 0, // 分页器总条数
        pageSize: 10, // 每页显示条数
        pageSizes: [10, 15, 20, 25], // 可选择的每页条数
        currentPage: 1 // 分页器当前页码
      },
      // 加载层参数配置
      loadingConfig: {
        target: 'body',
        fullscreen: true,
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.1)',
        text: '拼命加载中'
      },
      // 弹出层配置
      dialogConfig: {
        dialogEditMapVisible: false
      },
      // 编辑地图数据
      editMapForm: {
        mapId: '',
        mapStreet: '',
        mapName: '',
        mapDesc: '',
        mapBgm: ''
      },
      // 编辑地图表单验证
      editMapFormRules: {
        mapName: [
          { required: true, message: '请输入地图名称', trigger: 'blur' },
          {
            min: 1,
            max: 12,
            message: '地图名称在 1 到 12 个字符',
            trigger: 'blur'
          }
        ],
        mapDesc: [
          {
            min: 0,
            max: 255,
            message: '地图描述不超过255个字符',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    getMapList () {
      // 获取地图列表
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      this.$axios
        .get('/maplist', {
          params: {
            search: this.searchCont.trim(),
            pagenum: this.pageConfig.currentPage,
            pagesize: this.pageConfig.pageSize
          }
        })
        .then(res => {
          if (res.status === 0) {
            // 处理数据
            res.data.forEach(item => {
              if (item.map_status === 1) {
                item.map_status = true;
              } else {
                item.map_status = false;
              }
            });
            // 设置分页
            this.pageConfig.pageTotal = res.total;
            this.pageConfig.currentPage = res.pagenum;
            // 设置数据
            this.mapList = res.data;
            this.path = res.path;
            // 判断当前页是否为空
            if (
              this.pageConfig.pageTotal ===
              (this.pageConfig.currentPage - 1) * this.pageConfig.pagesize
            ) {
              if (this.pageConfig.currentPage - 1 < 1) {
                this.handleCurrentChange(1);
              } else {
                this.handleCurrentChange(this.pageConfig.currentPage - 1);
              }
            }
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    searchClear () {
      this.getMapList();
    },
    playMusic (url, name) {
      // 播放地图的BGM
      url = this.path + '/' + url;
      this.$emit('play-music', {
        url: url,
        name: name
      });
    },
    switchMap (obj) {
      // 地图开关切换
      var status = obj.map_status ? 1 : 0;
      this.$axios
        .put('/switchMap/' + obj.map_id, {
          mapStatus: status
        })
        .then(res => {
          if (res.status === 0) {
            // 提示成功信息
            this.$message.success(res.msg);
          } else {
            // 提示失败信息
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        });
    },
    editMap (id) {
      // 根据地图ID查找信息
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      // 发起请求
      this.$axios
        .get('/mapinfo/' + id)
        .then(res => {
          if (res.status === 0) {
            // 设置数据
            this.editMapForm.mapId = res.data.mapId;
            this.editMapForm.mapStreet = res.data.mapStreet;
            this.editMapForm.mapName = res.data.mapName;
            this.editMapForm.mapDesc = res.data.mapDesc;
            this.editMapForm.mapBgm = res.data.mapBgm;
            // 打开弹出层
            this.dialogConfig.dialogEditMapVisible = true;
          } else {
            // 提示失败信息
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    editMapSubmit (formName) {
      // 修改地图提交
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 打开加载层
          var loading = this.$loading.service(this.loadingConfig);
          this.$axios
            .put('/editMap/' + this.editMapForm.mapId, {
              mapName: this.editMapForm.mapName,
              mapDesc: this.editMapForm.mapDesc
            })
            .then(res => {
              if (res.status === 0) {
                // 关闭弹出层
                this.dialogConfig.dialogEditMapVisible = false;
                // 提示成功信息
                this.$message.success(res.msg);
                // 重新刷新数据
                this.getMapList();
              } else {
                // 提示失败信息
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              // 请求失败
              this.$message.error(error);
            })
            .finally(() => {
              // 关闭加载层
              this.$nextTick(() => {
                loading.close();
              });
            });
        }
      });
    },
    deleteMap (id) {
      // 删除地图
      // 调用确认框
      this.$confirm('你确定要删除该地图吗？', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      })
        .then(() => {
          // 打开加载层
          var loading = this.$loading.service(this.$store.state.loadingConfig);
          this.$axios
            .delete('/deleteMap/' + id)
            .then(res => {
              if (res.status === 0) {
                // 提示成功信息
                this.$message.success(res.msg);
                // 重新刷新数据
                this.getMapList();
              } else {
                // 提示失败信息
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              // 请求失败
              this.$message.error(error);
            })
            .finally(() => {
              // 关闭加载层
              this.$nextTick(() => {
                loading.close();
              });
            });
        })
        .catch(() => {});
    },
    handleSizeChange (val) {
      // 切换每页条数重新获取数据
      this.pageConfig.pageSize = val;
      this.getMapList();
    },
    handleCurrentChange (val) {
      // 切换页码时重新获取数据
      this.pageConfig.currentPage = val;
      this.getMapList();
    },
    errorImg (e) {
      console.log(e);
      e.srcElement.src = 'http://10.36.135.79:1314/map_mark/mark.Quest.png';
    }
  },
  mounted () {
    this.getMapList();
  }
};
</script>

<style lang="less" scoped>
.play-music {
  display: block;
  margin-left: 10px;
  color: #666666;
  font-size: 24px;
  text-decoration: none;
}
</style>
